package com.fulihui.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 微信文本消息工具类
 *
 * @author: Levon
 * @version: v 0.1 2017-05-23 17:01
 */
public class TextMsgUtil {

    static final String reg = "</?(?!a|/?a|a\\\\d)[^>]+>";

    public static String escape(String html) {
        if (StringUtils.isBlank(html)) {
            return html;
        }
        if (html.indexOf("</p>") > 1) {
            //将</p>标签替换成换行符
            html = html.replaceAll("</p>", "\n");
        }
        //去掉所有html标签，除了<a></a>
        html = html.replaceAll(reg, "")
                .replaceAll("target=\"_self\"", "")
                .replaceAll("target=\"_blank\"", "")
                .replaceAll("&nbsp;", "");
        return html;
    }
}
